package org.scboy.dbms.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.dbms.service.DictionaryService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("base/dictionary/")
@Menu("微服务管理系统.字典管理")
public class DictionaryController {
	
	@Autowired
	DictionaryService configService;
	
	@PostMapping("page")
	@Permission("分页查询字典")
	public Result page(@RequestBody Model params){
		Page page=configService.page(params);
		return Result.Success(page.getData(),page.getCount());	
	}
	
	@GetMapping("list")
	@Permission("查询启用字典列表")
	public Result list(){
		return Result.Success(configService.list(),"");	
	}

	/**
	 * 新增文件字典
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增字典")
	public Result add(@RequestBody Model params){
		if(configService.add(params)){
			return Result.Success("BASE_DICTIONARY_ADD_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARY_ADD_ERROR");
		}
	}
	
	/**
	 * 获取字典
	 * @param request
	 * @return
	 */
	@GetMapping("get")
	@Permission("获取字典详情")
	public Result get(String code){
		return Result.Success(configService.get(code),"");
	}
	
	/**
	 * 更新字典
	 * @param request
	 * @return
	 */
	@PutMapping("update")
	@Permission("更新字典")
	public Result update(@RequestBody Model params){
		if(configService.update(params)){
			return Result.Success("BASE_DICTIONARY_UPDATE_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARY_UPDATE_ERROR");
		}		
	}

	/**
	 * 删除字典
	 * @param request
	 * @return
	 */
	@DeleteMapping("delete")
	@Permission("删除字典")
	public Result delete(String code){
		if(configService.delete(code)){
			return Result.Success("BASE_DICTIONARY_DELETE_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARY_DELETE_ERROR");
		}
	}
	
	/**
	 * 启用禁用字典
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("checkstate")
	@Permission("启用禁用字典")
	public Result checkState(String code){
		if(configService.checkState(code)){
			return Result.Success("BASE_DICTIONARY_CHECKSTATE_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARY_CHECKSTATE_ERROR");
		}
	}
}
